package fg.tests;

import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;

import fg.animation.AnimationSequence;
import fg.animation.easing.ValueEasingAnimation;
import fg.math.Easing;

public class TestAnimation extends BasicGame {
	
	private AnimationSequence sequence;
	private float x;
	private float y;
	
	public TestAnimation() {
	    super("Test Animation");
    }

	@Override
    public void init(GameContainer container) throws SlickException {
		float startX = 10f;
		x = startX;
		y = 150f;
		
	    sequence = new AnimationSequence();
	    
	    // build animation to the right
	    ValueEasingAnimation animation = null;
	    animation = new ValueEasingAnimation(1000, startX, 400f, Easing.QUAD_IN_OUT);
	    animation.setListener(new ValueEasingAnimation.Listener() {
			public void onEvent(float value) {
	            x = value;
            }});
	    
	    sequence.add(animation);
	    
	    // build animation to the left
	    animation = null;
	    animation = new ValueEasingAnimation(500, 400f, startX, Easing.QUAD_IN_OUT);
	    animation.setListener(new ValueEasingAnimation.Listener() {
			public void onEvent(float value) {
	            x = value;
            }});
	    
	    sequence.add(animation);
	    
	    sequence.setLooping(true);
    }

	@Override
    public void update(GameContainer container, int delta) throws SlickException {
		sequence.update(delta);
		
		if (container.getInput().isKeyPressed(Input.KEY_SPACE)) {
			sequence.restart();
			sequence.setActive(!sequence.isActive());
		}
    }

	public void render(GameContainer container, Graphics g) throws SlickException {
		g.drawString("Press <SPACE>", 10f, 40f);
		g.drawRect(x, y, 20f, 20f);
    }	
	
}
